package com.east.controller;

import com.east.dto.ClueDTO;
import com.east.entity.Clue;
import com.east.query.ClueQuery;
import com.east.result.Result;
import com.east.service.ClueService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/clue/")
@EnableMethodSecurity
public class ClueController {
    @Resource
    private ClueService clueService;

    /**
     * 获取线索列表
     *
     * @param currentPage
     * @param pageSize
     * @param clueQuery
     * @return
     */
    @GetMapping("list")
    public Result list(@RequestParam(value = "currentPage", defaultValue = "1", required = false) Integer currentPage,
                       @RequestParam(value = "pageSize", defaultValue = "8", required = false) Integer pageSize,
                       @RequestParam(value = "fullName", required = false) ClueQuery clueQuery) {
        return clueService.list(currentPage, pageSize, clueQuery);
    }

    /**
     * 从excel导入线索信息
     *
     * @param file
     * @return
     */
    @PostMapping("importExcel")
    @PreAuthorize(value = "hasAuthority('clue:import')")
    public Result saveFromExcel(@RequestParam("file") MultipartFile file) {
        return clueService.saveFromExcel(file);
    }

    /**
     * 手动存入线索信息
     *
     * @param clueDTO
     * @return
     */
    @PostMapping("save")
    @PreAuthorize(value = "hasAuthority('clue:add')")
    public Result save(@RequestBody ClueDTO clueDTO) {
        return clueService.save(clueDTO);
    }

    /**
     * 验证手机号是否已经录入
     *
     * @param phone
     * @return
     */
    @GetMapping("phoneExists")
    public Result phoneExists(@RequestParam("phone") String phone,
                              @RequestParam(value = "clueId", required = false) Integer clueId) {
        return clueService.phoneExists(phone, clueId);
    }

    /**
     * 根据id获取线索信息
     *
     * @param id
     * @return
     */
    @GetMapping("clueInfo/{id}")
    public Result clueInfo(@PathVariable("id") Integer id) {
        return clueService.clueInfo(id);
    }

    /**
     * 根据id获取线索信息
     *
     * @param id
     * @return
     */
    @GetMapping("clueCascadeInfo/{id}")
    public Result clueCascadeInfo(@PathVariable("id") Integer id) {
        return clueService.clueCascadeInfo(id);
    }

    /**
     * 更新线索
     *
     * @param clue
     * @return
     */
    @PutMapping("update")
    @PreAuthorize(value = "hasAuthority('clue:edit')")
    public Result update(@RequestBody Clue clue) {
        return clueService.update(clue);
    }

    /**
     * 批量删除线索
     *
     * @param ids
     * @return
     */
    @PutMapping("delete")
    @PreAuthorize(value = "hasAuthority('clue:delete')")
    public Result batchDelete(@RequestBody Integer[] ids) {
        return clueService.batchDelete(ids);
    }
}
